package com.czw.ddm.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.czw.ddm.databinding.ActivitySplashBinding
import com.gyf.immersionbar.ImmersionBar
import com.mtzqc.common.app.App
import com.mtzqc.common.kt.onClick
import com.mtzqc.common.ui.activity.ViewBindingActivity
import com.mtzqc.common.utils.ThreadUtil

class SplashActivity : ViewBindingActivity<ActivitySplashBinding>(ActivitySplashBinding::inflate) {
    override fun afterCreate(savedInstanceState: Bundle?) {
        if ((intent?.flags ?: 0) and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
            return
        }
        super.afterCreate(savedInstanceState)
    }

    var launcher: Runnable? = null
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        if (App.isDev()) {
            LogUtils.e(AppUtils.getAppSignaturesSHA1().first())
            LogUtils.e(AppUtils.getAppSignaturesSHA256().first())
        }
        binding.jumpToMain.onClick {
            startMain()
        }
        launcher = ThreadUtil.runMainDelayed(20000) {
            startMain()
        }
    }

    private fun startMain() {
        ThreadUtil.remove(launcher)
        if (isDestroyed || isFinishing) {
            return
        }
        binding.centerImg.setImageDrawable(null)
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }


    override fun buildImmersionBar(tb: View?): ImmersionBar? {
        return super.buildImmersionBar(tb)?.fullScreen(true)
    }
}